﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.Kill;
using Server.API.DTOs;

namespace Server.API.Application.Handler.Kill
{
    public class GetKillHandler:IRequestHandler< GetKillCommand, APIResult<KillDTO>>
    {
        private readonly IRepository<KillModel> killway;
        private readonly IMapper mapper;
        private readonly ILogger<GetKillHandler> log;

        public GetKillHandler(IRepository<KillModel> killway, IMapper mapper, ILogger<GetKillHandler> log)
        {
            this.killway = killway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 获取屠宰数据
        /// </summary>
        /// <param name="request">屠宰主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>屠宰信息</returns>
        public async Task<APIResult<KillDTO>> Handle(GetKillCommand request, CancellationToken cancellationToken)
        {
            APIResult<KillDTO> res= new APIResult<KillDTO>();      
            var kill =await killway.GetValue(request.KillId);
            if (kill == null)
            {
                log.LogError("屠宰数据不存在");
                res.Num = StateCode.Fail;
                res.Message = "屠宰数据不存在";
            }
            else
            {
                log.LogInformation("获取成功");
                res.Data = mapper.Map<KillDTO>(kill);
                res.Num = StateCode.OK;
                res.Message = "获取成功";
            }
            return res;
        }
    }
}
